cmake_minimum_required(VERSION 3.10 FATAL_ERROR)

project(qelib
    VERSION 1.3.3
    DESCRIPTION "Quantum Embeded Library"
)

include(cmake/top.cmake)

if(CMAKE_CROSSCOMPILING)
    message(STATUS "Cross building")
else()
    message(STATUS "Host building")
endif()
message(STATUS "ARCH:${CMAKE_SYSTEM_PROCESSOR} OS:${CMAKE_SYSTEM_NAME}")

#
# If ARCH not set, it is local compilation, and ARCH is set to host arch
#
if (ARCH)
    message(STATUS "Target arch ${ARCH}")
else()
    set(HOST_COMPILE 1)
    if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86")
        set(ARCH x86)
        set(CONFIG_X86 1)
    elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "AMD64")
        set(ARCH x86)
        set(CONFIG_X86 1)
    elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm")
        set(ARCH arm)
        set(CONFIG_ARM 1)
    else()
        message(STATUS "Processor ${CMAKE_SYSTEM_PROCESSOR}")
    endif()
endif()

#
# Releae/Debug
# ======================
#

if(CONFIG_BUILD_DEBUG)
    
    message(STATUS "Build debug")
    
    set(CMAKE_BUILD_TYPE "Debug")
    
    set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -g")

    if(CONFIG_BUILD_OPTIMAZATION_O0)
        message(STATUS "Optimizate for space")
        set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -O0")
    elseif(CONFIG_BUILD_OPTIMAZATION_O1)
        set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -O1")
    elseif(CONFIG_BUILD_OPTIMAZATION_O2)
        set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -O2")
    elseif(CONFIG_BUILD_OPTIMAZATION_O3)
        set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -O3")
    elseif(CONFIG_BUILD_OPTIMAZATION_OS)
        message(STATUS "Optimizate for size")
        set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -Os")
    endif()
else()
    
    message(STATUS "Build release")
    
    set(CMAKE_BUILD_TYPE "Release")

    if(CONFIG_BUILD_OPTIMAZATION_O0)
        message(STATUS "Optimizate for space")
        set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O0")
    elseif(CONFIG_BUILD_OPTIMAZATION_O1)
        set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O1")
    elseif(CONFIG_BUILD_OPTIMAZATION_O2)
        set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O2")
    elseif(CONFIG_BUILD_OPTIMAZATION_O3)
        set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O3")
    elseif(CONFIG_BUILD_OPTIMAZATION_OS)
        message(STATUS "Optimizate for size")
        set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -Os")
    endif()
endif()

#
# library define
# ======================
#
if(CONFIG_BUILD_SHARED)
    message(STATUS "Build shared libaray")
else()
    message(STATUS "Build static libaray")
endif()
set(QELIB qe-${BUILD_NAME})
message(STATUS "Library ${QELIB}")
qe_library(${QELIB})
qelib_include_directories(${AUTOGEN_DIR})
add_subdirectory(arch)
add_subdirectory(common)
add_subdirectory(lib)
add_subdir_ifdef(CONFIG_ISP isp)
add_subdir_ifdef(CONFIG_NET net)
add_subdir_ifdef(CONFIG_DM  driver)
add_subdir_ifdef(CONFIG_CMD command)

get_target_property(QE_LIBRARY_SOURCES ${QELIB} SOURCES)
if(VERBOSE)
    message(STATUS "Build sources:")
    foreach(src ${QE_LIBRARY_SOURCES})
        message(STATUS ${src})
    endforeach()
endif()

# apps
if (HOST_COMPILE)
    add_subdir_ifdef(CONFIG_APP apps)
endif()

# install
set(QELIB_INSTALL_PATH ${PROJECT_ROOT}/release/${PROJECT_VERSION}/${BUILD_NAME})

# install header

if(VERBOSE)
    message(STATUS "Install headers")
    foreach(header ${QELIB_INSTALL_HEADERS})
        message(STATUS ${header})
    endforeach()
endif()

qelib_install_headers(${AUTOGEN_DIR}/qe_version.h)
qelib_install_headers(${AUTOGEN_DIR}/autoconfig.h)
qelib_install_headers(${AUTOGEN_DIR}/autocheck.h)

install(FILES ${QELIB_INSTALL_HEADERS} DESTINATION ${QELIB_INSTALL_PATH}/include)
install(TARGETS ${QELIB} DESTINATION ${QELIB_INSTALL_PATH}/lib)
if(SPHINX_EXECUTABLE)
install(DIRECTORY ${PROJECT_ROOT}/docs/sphinx_html DESTINATION ${QEDOC_INSTALL_PATH}/doc)
endif()

list(APPEND CMAKE_MODULE_PATH "${PROJECT_ROOT}/cmake")

# Build Sphinx Document
if(SPHINX_EXECUTABLE)
    include(cmake/sphinxdoc.cmake)
    add_sphinx_doc(
    SOURCE_DIR
        ${PROJECT_ROOT}/docs/source
    BUILD_DIR
        ${PROJECT_ROOT}/docs/source
    CACHE_DIR
        ${PROJECT_ROOT}/docs/_doctrees
    HTML_DIR
        ${PROJECT_ROOT}/docs/sphinx_html
    TARGET_NAME
        docs
    COMMENT
        "HTML documentation"
    )
endif()
